<?php
  require_once('db_connection.php');

  $id = isset($_GET['id'])?$_GET['id']:0;
  $search = $s_where = '';

# uncomment following code for server variables
//pr($_SERVER);
#$qs = isset($_SERVER['QUERY_STRING'])?'?'.$_SERVER['QUERY_STRING']:'';

  pr($_GET);
  pr($_POST);
  /*********** SEARCH **************/
  if(isset($_GET['search']) && !empty($_GET['search'])){
    $search = trim($_GET['search']);
    $s_where = "`name` LIKE '%$search%' OR `email` LIKE '%$search%'";
  }
  /*********************************/
  // want to delete by checkbox
  if(isset($_POST['chk'])){
    $ids = implode(',',$_POST['chk']);
    pr($ids);
    $chk_where = "`id` IN($ids)";
    $delRows = delete('products',$chk_where);
    pr($delRows);
  }
  if(isset($_GET['mode']) && $_GET['mode']=='del') {

    $deletedRows = deleteById('products',$id);
    pr($deletedRows);
  }


  // if set in URL then update
  if(isset($_GET['status']) && isset($_GET['id'])) {
    $url_status = ($_GET['status'] == 1) ? 0 : 1;

    /******* start query  *********/
    $data = array('status'=>$url_status);
    $u_where = "`id`= '$id'";
    $updated_rows = update('products',$data,$u_where);
    /******* end query  *********/
    if($updated_rows == 1){
      echo "<h4>Status changed successfully..</h4>";
    }
  }

  //////////////////////////////
  $curPage = isset($_GET['page']) && !empty($_GET['page']) ? $_GET['page'] : 1;
  $totalrecords = countAll('products',$s_where);
  $rpp = 4;// Record Per Page
  $limit = ($curPage-1)*$rpp;
pr($totalrecords);
  $limitClause = "$limit,$rpp";
  $totalPages = ceil( $totalrecords / $rpp );
  /******* SORTING ***/
  $field = isset($_GET['field']) && !empty($_GET['field']) ? $_GET['field'] : 'added_date';
  $order = isset($_GET['order']) && !empty($_GET['order']) ? $_GET['order'] : 'DESC';
  $site_orders = array('asc','desc');
  if(!in_array(strtolower($order), $site_orders)){
    $order = 'desc';
  }
  $newOrder = ($order=='asc')?'desc':'asc';

  /******* SORTING ***/
  $result  = fetchAll('products',$s_where,"`$field` $order",$limitClause);
  //$result  = fetchAll('products','','',$limitClause);
  //////////////////////////////
  $selected_records = mysql_num_rows($result);
  echo "<h4>TOTAL : $totalrecords </h4>";
  echo "<h4>On This Page :$selected_records </h4>";

?>
<link href="../html/css/site.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.8.2.min.js" type="text/javascript"></script>

<form>
  <input type="search" name="search" placeholder="Search Here.." />
  <input type="submit" value="Search" />
</form>
<a href="add_product.html">Add New</a>
<form action="" method="post">
  <table width="80%" cellspacing="0" cellpadding="0">
    <thead>
      <tr>
        <th><input type="checkbox" id="chk_pid" name="c_all" value="c_parent" /></th>
        <th><a href="?page=<?=$curPage?>&order=<?=$newOrder?>&field=id&search=<?=$search?>">ID</a></th>
        <th>NAME</th>
        <th>Image</th>
        <th>ADDED DATE</th>
        <th><a href="?page=<?=$curPage?>&order=<?=$newOrder?>&field=status&search=<?=$search?>">STATUS</a></th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
  <?php
    while($ans = mysql_fetch_assoc($result)){
      $status = getStatus($ans['status']);
      $url = "?id=$ans[id]&status=$ans[status]&page=$curPage&search=$search";
      $status = '<a href="'.$url.'">'.$status.'</a>';
  ?>
      <tr>
        <td><input type="checkbox" class="chk" name="chk[]" value="<?=$ans['id']?>" /></td>
        <td><a href="show_user.php?id=<?=$ans['id']?>"><?=$ans['id']?></a></td>
        <td><?=$ans['name']?></td>
        <td><img height="90" width="120" src="p_images/<?=$ans['image']?>"><?=$ans['image']?></td>
        <td><?=date('d-M-Y h:i:s a',strtotime($ans['added_date']))?></td>
        <td><?=$status?></td>
        <td><a href="?mode=del&id=<?=$ans['id']?>&page=<?=$curPage?>&search=<?=$search?>" onClick="return confirm('Are you sure to delete?');">Delete</a></td>
      </tr>
  <?php
      //print_r($ans);
    }
  ?>


    </tbody>
  </table>
  <input type="submit" value="Delete All" id="delAllId" name="delAll" />  <br>
</form>

  <table  border="0" width="80%" align="left" cellspacing ="0">
      <tr>
<?php
for ($i = 1; $i <= $totalPages ; $i++)
{
  if($i==$curPage){
?>

   <th style="font-size:30px;background:#fff;"><?=$i?></th>
<?php
  } else {
?>
        <th><a href="?page=<?=$i?>&order=<?=$order?>&field=<?=$field?>&search=<?=$search?>"><?=$i?></a></th>
<?php
  }//else
}
?>
      </tr>
  </table>
<br><br><br>
<script type="text/javascript">
  $(document).ready(function(){

    $('#chk_pid').on('click',function(){
    //alert($(this).is(':checked'));
      var action = $(this).is(':checked');
      $('.chk').each(function(i,j){
        //console.log(i);
        //console.log(j);
        $(this).prop('checked',action);
      })
    });//end click #chk_pid


    /*****
    * Child Checkbox Checked/Uncheck
    */
    $('.chk').on('click',function(){

      var action = $(this).is(':checked');
      var flag = false;
      if(action == true){
        var allchk = $('.chk').size();
        var allChildChk = $('.chk:checked').size();
        if(allchk == allChildChk){
          var flag = true;
        }
      }

      $('#chk_pid').prop('checked',flag);

    });//end click .chk

    $('input[type="submit"]#delAllId').on('click',function(){

      var allChildChk = $('.chk:checked').size();

      if(allChildChk == 0){
        alert("Please check atleast one checkbox");
        return false;
      }

      var flag = confirm('Are you sure to delete '+allChildChk +' user(s) ?');

      // user didn't want to delete
      if(flag == false) {
        return false;
      }


    });

  });
</script>
